package processingdata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.geonames.ToponymSearchCriteria;
import org.geonames.ToponymSearchResult;
import org.geonames.WebService;
import org.xml.*;




public class TransferData {
	
	
	public static void main(String args[])throws Exception
	{
		int numOfSightings=0;
		ArrayList<String> exclusions=new ArrayList<String>();
		HashMap<String,Double> latitudeMap=new HashMap<String,Double>();
		HashMap<String,Double> longitudeMap=new HashMap<String,Double>();
		long population;
		HashMap<String,Long> populationMap=new HashMap<String, Long>();
				
		HashMap<String,Integer> shapeMap=new HashMap<String,Integer>();
		
		int counter=0;
		
		String fileName="project3cs424/split_files/split_part9";
		File file=new File(fileName);	
		BufferedReader inputReader=new BufferedReader(new FileReader(file));
		String outputFileName=fileName.split("/")[2].split("_")[1];
		BufferedWriter outputWriter=new BufferedWriter(new FileWriter(new File("project3cs424/geofound/ufo"+outputFileName+".data")));
		while(inputReader.ready())
		{
			
			numOfSightings++;
			if(numOfSightings==1500)
			{
				counter++;
				numOfSightings=0;
			}
			String inputLine=inputReader.readLine();
			
			ToponymSearchCriteria searchCriteria=new ToponymSearchCriteria();
			WebService.setUserName("vivek");
				
			//outputWriter.write(inputLine+"\n");
			String[] inputLineParts=inputLine.split("###");
			String date=inputLineParts[0];
			String city=inputLineParts[1];
			
			double latitude;
			double longitude;
			
			
			String state=inputLineParts[2];
			System.out.println(city+" "+state+" "+numOfSightings);
			if(latitudeMap.containsKey(city) && longitudeMap.containsKey(city)&&populationMap.containsKey(city))
			{
				latitude=latitudeMap.get(city);
				longitude=longitudeMap.get(city);
				population=populationMap.get(city);
			}
			else
			{
				searchCriteria.setQ(city+" "+state);
				//searchCriteria.setName(city);
				searchCriteria.setCountryCode("US");
				
				ToponymSearchResult searchResults=WebService.search(searchCriteria);
//				for(Toponym toponym:searchResults.getToponyms())
//				{
//					System.out.println(toponym.getName()+" "+toponym.getFeatureClassName()+" "+toponym.getFeatureCodeName()+" "+toponym.getLatitude()+" "+toponym.getLongitude()+" "+toponym.getCountryName());
//				}
				
				if(searchResults.getTotalResultsCount()==0)
				{
					searchCriteria.setQ(city);
					searchResults=WebService.search(searchCriteria);
				}
				
				
				if(searchResults.getTotalResultsCount()==0)
				{
					latitude=0;
					latitudeMap.put(city, latitude);
					longitude=0;
					longitudeMap.put(city, longitude);
					population=0;
					populationMap.put(city, population);
				}
				else
				{
					latitude=searchResults.getToponyms().get(0).getLatitude();
					latitudeMap.put(city, latitude);
					longitude=searchResults.getToponyms().get(0).getLongitude();
					longitudeMap.put(city, longitude);
					
				}
				
			}
			
			
			outputWriter.write(inputLine+"###"+latitude+"###"+longitude+"\n");
			
		}
		outputWriter.close();
		
			
	}
	
	public static void getFilesInDirectory(String dirName,ArrayList<String> files)
	{
		File dir=new File(dirName);
		
		//System.out.println(dir);
		if(dir.isDirectory())
		{
			if(dir.getName().contains(".svn"))
			{
				//do nothing
			}
			else
			{
			
				File[] fileArray=dir.listFiles();
							
				for(int i=0;i<fileArray.length;i++)
				{
				//	System.out.println(fileArray[i]);
					getFilesInDirectory(fileArray[i].getAbsolutePath(),files);
				}

			}
					}
		else if(dir.isFile())
		{
			files.add(dir.getAbsolutePath());
		}
		
		
	}

}
